package com.tpshion.deploy.task.support;

import java.util.List;
import java.util.stream.Collectors;

public class TaskRunnable implements Runnable{

    private volatile boolean running = true;

    private TaskContext taskContext;

    private TaskFactory taskFactory;

    public TaskRunnable(TaskContext taskContext, TaskFactory taskFactory){
        this.taskContext = taskContext;
        this.taskFactory = taskFactory;
    }

    @Override
    public void run() {
        List<TaskData> collect = taskContext.getList().stream().filter(i -> i.getSort() >= taskContext.getTaskIndex())
                .collect(Collectors.toList());

        for (TaskData taskData : collect) {
            if(!running){
                return;
            }
            taskFactory.getTaskHandler(taskData.getTaskType()).startHandleTask(taskData);
        }
    }

    public void stop(){
        running = false;
    }
}
